const { ListNode, LinkedList } = require('./1. 建立线性链表')

// 1. 链表头部删除元素

function removeFront(linkedList) {
    if (linkedList.head) {
        linkedList.head = linkedList.head.next
    }
}
let linkedList1 = new LinkedList([2, 3, 5, 6, 8, 1])
removeFront(linkedList1)
// 2. 链表尾部删除元素
function removeRear(linkedList) {
    if (!linkedList.head.next) {
        return 'Error'
    }
    let cur = linkedList.head
    while (cur.next.next) {
        cur = cur.next
    }
    cur.next = null
}
let linkedList2 = new LinkedList([2, 3, 5, 6, 8, 1])
removeRear(linkedList2)

// 3. 链表中间删除某个元素
function removeInside(linkedList, index) {
    let count = 0
    let cur = linkedList.head
    while (cur.next && count < index - 1) {
        count += 1
        cur = cur.next
    }
    if (!cur) {
        return 'Error'
    }
    let delNode = cur.next
    cur.next = delNode.next
}
let linkedList3 = new LinkedList([2, 3, 5, 6, 8, 1])
removeInside(linkedList3, 1)
console.log(linkedList3)